﻿using Microsoft.Xna.Framework;
using SceneEngine;

namespace SceneEditor
{
    public class RotateAction : EditAction
    {
        private string[] targets;

        private bool localRotation;

        private Quaternion deltaRotation;
        private Vector3[] deltaPosition;

        public RotateAction(string[] targets, bool localRotation, Quaternion deltaRotation, Vector3[] deltaPosition)
        {
            this.localRotation = localRotation;

            this.targets = targets;

            this.deltaRotation = deltaRotation;
            this.deltaPosition = deltaPosition;
        }

        public override void Undo()
        {
            for (int i = 0; i < targets.Length; i++)
            {
                BaseObject entity = EngineGlobals.Scene.GetBaseObject(targets[i]);

                if (localRotation)
                {
                    entity.Rotation = entity.Rotation * Quaternion.Inverse(deltaRotation);
                }
                else
                {
                    entity.Rotation = Quaternion.Inverse(deltaRotation) * entity.Rotation;
                }

                entity.Position -= deltaPosition[i];
            }
        }

        public override void Redo()
        {
            for (int i = 0; i < targets.Length; i++)
            {
                BaseObject entity = EngineGlobals.Scene.GetBaseObject(targets[i]);

                if (localRotation)
                {
                    entity.Rotation = entity.Rotation * deltaRotation;
                }
                else
                {
                    entity.Rotation = deltaRotation * entity.Rotation;
                }

                //entity.Rotation += deltaRotation;
                entity.Position += deltaPosition[i];
            }
        }
    }
}
